{% extends 'admin_dashboard.html' %}
{% block content %}
<div class="card">
  <h2>{{ '编辑' if deposit else '添加' }}存款业务</h2>
  <form id="depositForm" method="POST">
    <div class="form-group">
      <label>存款编号</label>
      <input type="text" name="savingID" value="{{ deposit.savingID if deposit else '' }}" {% if deposit %}readonly{% endif %} required>
    </div>
    <div class="form-group">
      <label>存款名称</label>
      <input type="text" name="savingName" value="{{ deposit.savingName if deposit else '' }}" required>
    </div>
    <div class="form-group">
      <label>存款描述</label>
      <input type="text" name="descrip" value="{{ deposit.descrip if deposit else '' }}">
    </div>

    <div class="form-group">
      <label>客户编号</label>
      <select name="customerID" required>
        <option value="">请选择客户</option>
        {% for customer in customers %}
        <option value="{{ customer.customerID }}"
          {% if deposit and deposit.customerID == customer.customerID %}selected{% endif %}>
          {{ customer.customerID }} - {{ customer.customerName }}
        </option>
        {% endfor %}
      </select>
    </div>

    <button type="submit" class="btn primary">提交</button>
    <a href="{{ url_for('deposits') }}" class="btn">返回</a>
  </form>
</div>

<script>
document.getElementById('depositForm').addEventListener('submit', function(e) {
  e.preventDefault();

  const formData = new FormData(this);

  fetch('{{ url_for("add_deposit") }}', {
    method: 'POST',
    body: formData
  }).then(res => res.json())
    .then(data => {
      if (data.success) {
        alert('✅ 添加成功！');
        window.location.href = '{{ url_for("deposits") }}';
      } else {
        alert('❌ ' + data.message);
      }
    }).catch(() => {
      alert('⚠️ 提交失败，请稍后再试。');
    });
});
</script>
{% endblock %}
